package comp512.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Image;

import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.SwingConstants;
import javax.swing.border.BevelBorder;
import javax.swing.border.EmptyBorder;

import comp512.classes.DB;
import comp512.classes.Question;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

@SuppressWarnings("serial")
public class ServerGUI_QuestionBank extends JFrame {

	private JPanel contentPane;
	private JSplitPane splitPane;
	private JPanel panel;
	private JButton btnPrevious;
	private JLabel lblQuestion;
	private JPanel pnlLeftButtons;
	private JButton btnNext;
	private JButton btnNew;
	private JSplitPane splitAnswerPane;
	private JPanel pnlAnswersLeft;
	private JLabel lblAns1, lblAns2, lblAns3, lblAns4, lblAns5, lblAns6,
			lblAns7, lblAns8, lblAns9, lblAns10;
	private JLabel lblPts1, lblPts2, lblPts3, lblPts4, lblPts5, lblPts6,
			lblPts7, lblPts8, lblPts9, lblPts10;
	private int currentQuestion = 0;
	private DB database = DB.getInstance();
	private int qCount = database.getQuestionCount();

	/**
	 * Create the frame.
	 */
	public ServerGUI_QuestionBank() {
		setType(Type.UTILITY);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);

		splitPane = new JSplitPane();
		splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
		contentPane.add(splitPane, BorderLayout.CENTER);

		panel = new JPanel();
		panel.setMinimumSize(new Dimension(10, 50));
		splitPane.setLeftComponent(panel);

		btnPrevious = new JButton("");
		btnPrevious.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (currentQuestion == 0)
					currentQuestion = qCount - 1;
				else
					currentQuestion--;
				updateQuestion();
			}
		});
		btnPrevious.setToolTipText("Previous Question");
		btnPrevious.setBorder(new BevelBorder(BevelBorder.RAISED, null, null,
				null, null));
		btnPrevious.setIconTextGap(0);
		btnPrevious.setMaximumSize(new Dimension(50, 50));
		btnPrevious.setMinimumSize(new Dimension(50, 50));
		btnPrevious.setBackground(Color.WHITE);

		ImageIcon imgIconPrevious = new ImageIcon(
				ServerGUI_QuestionBank.class
						.getResource("/comp512/resources/Previous.png"));
		Image imgPrevious = imgIconPrevious.getImage();
		Image fixedSizePrevious = imgPrevious.getScaledInstance(40, 40,
				Image.SCALE_SMOOTH);
		imgIconPrevious = new ImageIcon(fixedSizePrevious);
		panel.setLayout(new BorderLayout(0, 0));
		btnPrevious.setIcon(imgIconPrevious);
		panel.add(btnPrevious, BorderLayout.WEST);

		lblQuestion = new JLabel(
				"<html><p>Tell me something specific you have lost more than once Tell me something specific you have lost more than once</p></html>");
		lblQuestion.setToolTipText("Question");
		lblQuestion.setBorder(new EmptyBorder(0, 5, 0, 5));
		lblQuestion.setHorizontalAlignment(SwingConstants.CENTER);
		panel.add(lblQuestion, BorderLayout.CENTER);

		pnlLeftButtons = new JPanel();
		pnlLeftButtons.setPreferredSize(new Dimension(100, 50));
		pnlLeftButtons.setMaximumSize(new Dimension(100, 50));
		pnlLeftButtons.setMinimumSize(new Dimension(100, 50));
		panel.add(pnlLeftButtons, BorderLayout.EAST);
		pnlLeftButtons.setLayout(new BorderLayout(0, 0));

		btnNext = new JButton("");
		btnNext.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (currentQuestion == (qCount - 1))
					currentQuestion = 0;
				else
					currentQuestion++;
				updateQuestion();
			}
		});
		btnNext.setToolTipText("Next Question");
		btnNext.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null,
				null));
		btnNext.setMaximumSize(new Dimension(50, 50));
		btnNext.setMinimumSize(new Dimension(50, 50));
		btnNext.setPreferredSize(new Dimension(50, 50));

		ImageIcon imgIconNext = new ImageIcon(
				ServerGUI_QuestionBank.class
						.getResource("/comp512/resources/Next.png"));
		Image imgNext = imgIconNext.getImage();
		Image fixedSizeNext = imgNext.getScaledInstance(40, 40,
				Image.SCALE_SMOOTH);
		imgIconNext = new ImageIcon(fixedSizeNext);
		btnNext.setIcon(imgIconNext);
		pnlLeftButtons.add(btnNext, BorderLayout.WEST);

		btnNew = new JButton("");
		btnNew.setToolTipText("New Question");
		btnNew.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null,
				null));
		btnNew.setPreferredSize(new Dimension(50, 50));
		btnNew.setMinimumSize(new Dimension(50, 50));
		btnNew.setMaximumSize(new Dimension(50, 50));
		ImageIcon imgIconNew = new ImageIcon(
				ServerGUI_QuestionBank.class
						.getResource("/comp512/resources/New.png"));
		Image imgNew = imgIconNew.getImage();
		Image fixedSizeNew = imgNew.getScaledInstance(40, 40,
				Image.SCALE_SMOOTH);
		imgIconNew = new ImageIcon(fixedSizeNew);
		btnNew.setIcon(imgIconNew);
		pnlLeftButtons.add(btnNew, BorderLayout.EAST);

		splitAnswerPane = new JSplitPane();
		splitAnswerPane.setResizeWeight(0.5);
		splitPane.setRightComponent(splitAnswerPane);

		pnlAnswersLeft = new JPanel();
		splitAnswerPane.setLeftComponent(pnlAnswersLeft);
		pnlAnswersLeft
				.setLayout(new BoxLayout(pnlAnswersLeft, BoxLayout.Y_AXIS));

		JPanel pnlA1 = new JPanel();
		pnlAnswersLeft.add(pnlA1);
		pnlA1.setLayout(new BorderLayout(0, 0));

		JLabel lblID1 = new JLabel("1.");
		lblID1.setBorder(new EmptyBorder(0, 5, 0, 5));
		pnlA1.add(lblID1, BorderLayout.WEST);

		lblPts1 = new JLabel("000");
		lblPts1.setBorder(new EmptyBorder(0, 5, 0, 5));
		pnlA1.add(lblPts1, BorderLayout.EAST);

		lblAns1 = new JLabel("Answer 1");
		pnlA1.add(lblAns1, BorderLayout.CENTER);

		JPanel pnlA2 = new JPanel();
		pnlAnswersLeft.add(pnlA2);
		pnlA2.setLayout(new BorderLayout(0, 0));

		JLabel lblID2 = new JLabel("2.");
		lblID2.setBorder(new EmptyBorder(0, 5, 0, 5));
		pnlA2.add(lblID2, BorderLayout.WEST);

		lblPts2 = new JLabel("000");
		lblPts2.setBorder(new EmptyBorder(0, 5, 0, 5));
		pnlA2.add(lblPts2, BorderLayout.EAST);

		lblAns2 = new JLabel("Answer 1");
		pnlA2.add(lblAns2, BorderLayout.CENTER);

		JPanel pnlA3 = new JPanel();
		pnlAnswersLeft.add(pnlA3);
		pnlA3.setLayout(new BorderLayout(0, 0));

		JLabel lblID3 = new JLabel("3.");
		lblID3.setBorder(new EmptyBorder(0, 5, 0, 5));
		pnlA3.add(lblID3, BorderLayout.WEST);

		lblPts3 = new JLabel("000");
		lblPts3.setBorder(new EmptyBorder(0, 5, 0, 5));
		pnlA3.add(lblPts3, BorderLayout.EAST);

		lblAns3 = new JLabel("Answer 1");
		pnlA3.add(lblAns3, BorderLayout.CENTER);

		JPanel pnlA4 = new JPanel();
		pnlAnswersLeft.add(pnlA4);
		pnlA4.setLayout(new BorderLayout(0, 0));

		JLabel lblID4 = new JLabel("4.");
		lblID4.setBorder(new EmptyBorder(0, 5, 0, 5));
		pnlA4.add(lblID4, BorderLayout.WEST);

		lblPts4 = new JLabel("000");
		lblPts4.setBorder(new EmptyBorder(0, 5, 0, 5));
		pnlA4.add(lblPts4, BorderLayout.EAST);

		lblAns4 = new JLabel("Answer 1");
		pnlA4.add(lblAns4, BorderLayout.CENTER);

		JPanel pnlA5 = new JPanel();
		pnlAnswersLeft.add(pnlA5);
		pnlA5.setLayout(new BorderLayout(0, 0));

		JLabel lblID5 = new JLabel("5.");
		lblID5.setBorder(new EmptyBorder(0, 5, 0, 5));
		pnlA5.add(lblID5, BorderLayout.WEST);

		lblPts5 = new JLabel("000");
		lblPts5.setBorder(new EmptyBorder(0, 5, 0, 5));
		pnlA5.add(lblPts5, BorderLayout.EAST);

		lblAns5 = new JLabel("Answer 1");
		pnlA5.add(lblAns5, BorderLayout.CENTER);

		JPanel pnlAnswersRight = new JPanel();
		splitAnswerPane.setRightComponent(pnlAnswersRight);
		pnlAnswersRight.setLayout(new BoxLayout(pnlAnswersRight,
				BoxLayout.Y_AXIS));

		JPanel pnlA6 = new JPanel();
		pnlAnswersRight.add(pnlA6);
		pnlA6.setLayout(new BorderLayout(0, 0));

		JLabel lblID6 = new JLabel("6.");
		lblID6.setBorder(new EmptyBorder(0, 5, 0, 5));
		pnlA6.add(lblID6, BorderLayout.WEST);

		lblPts6 = new JLabel("000");
		lblPts6.setBorder(new EmptyBorder(0, 5, 0, 5));
		pnlA6.add(lblPts6, BorderLayout.EAST);

		lblAns6 = new JLabel("Answer 6");
		pnlA6.add(lblAns6, BorderLayout.CENTER);

		JPanel pnlA7 = new JPanel();
		pnlAnswersRight.add(pnlA7);
		pnlA7.setLayout(new BorderLayout(0, 0));

		JLabel lblID7 = new JLabel("7.");
		lblID7.setBorder(new EmptyBorder(0, 5, 0, 5));
		pnlA7.add(lblID7, BorderLayout.WEST);

		lblPts7 = new JLabel("000");
		lblPts7.setBorder(new EmptyBorder(0, 5, 0, 5));
		pnlA7.add(lblPts7, BorderLayout.EAST);

		lblAns7 = new JLabel("Answer 7");
		pnlA7.add(lblAns7, BorderLayout.CENTER);

		JPanel pnlA8 = new JPanel();
		pnlAnswersRight.add(pnlA8);
		pnlA8.setLayout(new BorderLayout(0, 0));

		JLabel lblID8 = new JLabel("8.");
		lblID8.setBorder(new EmptyBorder(0, 5, 0, 5));
		pnlA8.add(lblID8, BorderLayout.WEST);

		lblPts8 = new JLabel("000");
		lblPts8.setBorder(new EmptyBorder(0, 5, 0, 5));
		pnlA8.add(lblPts8, BorderLayout.EAST);

		lblAns8 = new JLabel("Answer 8");
		pnlA8.add(lblAns8, BorderLayout.CENTER);

		JPanel pnlA9 = new JPanel();
		pnlAnswersRight.add(pnlA9);
		pnlA9.setLayout(new BorderLayout(0, 0));

		JLabel lblID9 = new JLabel("9.");
		lblID9.setBorder(new EmptyBorder(0, 5, 0, 5));
		pnlA9.add(lblID9, BorderLayout.WEST);

		lblPts9 = new JLabel("000");
		lblPts9.setBorder(new EmptyBorder(0, 5, 0, 5));
		pnlA9.add(lblPts9, BorderLayout.EAST);

		lblAns9 = new JLabel("Answer 9");
		pnlA9.add(lblAns9, BorderLayout.CENTER);

		JPanel pnlA10 = new JPanel();
		pnlAnswersRight.add(pnlA10);
		pnlA10.setLayout(new BorderLayout(0, 0));

		JLabel lblID10 = new JLabel("10.");
		lblID10.setBorder(new EmptyBorder(0, 0, 0, 5));
		pnlA10.add(lblID10, BorderLayout.WEST);

		lblPts10 = new JLabel("000");
		lblPts10.setBorder(new EmptyBorder(0, 5, 0, 5));
		pnlA10.add(lblPts10, BorderLayout.EAST);

		lblAns10 = new JLabel("Answer 10");
		pnlA10.add(lblAns10, BorderLayout.CENTER);
		getFirstQuestion();
	}

	private void getFirstQuestion() {
		currentQuestion = 0;
		updateQuestion();
	}

	private void updateQuestion() {
		try {
			Question q = database.getQuestion(currentQuestion);
			lblQuestion.setText("<html><p>" + q.getQuestion() + "</p></html>");
			int answers = q.getAnswerCount();
			System.out.println("Question: " + q.getQuestion() + "\nAnswers: "
					+ answers);
			switch (answers) {
			case 10:
				lblAns10.setText(q.getAnswer(9).getAnswer());
				lblPts10.setText(q.getAnswer(9).getWeightString());
			case 9:
				lblAns9.setText(q.getAnswer(8).getAnswer());
				lblPts9.setText(q.getAnswer(8).getWeightString());
			case 8:
				lblAns8.setText(q.getAnswer(7).getAnswer());
				lblPts8.setText(q.getAnswer(7).getWeightString());
			case 7:
				lblAns7.setText(q.getAnswer(6).getAnswer());
				lblPts7.setText(q.getAnswer(6).getWeightString());
			case 6:
				lblAns6.setText(q.getAnswer(5).getAnswer());
				lblPts6.setText(q.getAnswer(5).getWeightString());
			case 5:
				lblAns5.setText(q.getAnswer(4).getAnswer());
				lblPts5.setText(q.getAnswer(4).getWeightString());
			case 4:
				lblAns4.setText(q.getAnswer(3).getAnswer());
				lblPts4.setText(q.getAnswer(3).getWeightString());
			case 3:
				lblAns3.setText(q.getAnswer(2).getAnswer());
				lblPts3.setText(q.getAnswer(2).getWeightString());
			case 2:
				lblAns2.setText(q.getAnswer(1).getAnswer());
				lblPts2.setText(q.getAnswer(1).getWeightString());
			case 1:
				lblAns1.setText(q.getAnswer(0).getAnswer());
				lblPts1.setText(q.getAnswer(0).getWeightString());
			}
			switch (answers + 1) {
			case 2:
				lblAns2.setText("   ");
				lblPts2.setText("   ");
			case 3:
				lblAns3.setText("   ");
				lblPts3.setText("   ");
			case 4:
				lblAns4.setText("   ");
				lblPts4.setText("   ");
			case 5:
				lblAns5.setText("   ");
				lblPts5.setText("   ");
			case 6:
				lblAns6.setText("   ");
				lblPts6.setText("   ");
			case 7:
				lblAns7.setText("   ");
				lblPts7.setText("   ");
			case 8:
				lblAns8.setText("   ");
				lblPts8.setText("   ");
			case 9:
				lblAns9.setText("   ");
				lblPts9.setText("   ");
			case 10:
				lblAns10.setText("   ");
				lblPts10.setText("   ");
			}

		} catch (NullPointerException npx) {
			System.out.println("DB null? " + (database == null));
			System.out.println("Questions: " + database.getQuestionCount());
			System.out.println("Null Pointer: " + npx.getMessage()
					+ npx.getCause());
			System.exit(-1);
		}
	}

}
